import { nodeTypeNames, clumn1, clumn2, clumn3, clumn4, clumn5 } from "./show";
import {
  initiatingNodeConfig,
  dispatchNodeConfig,
  handlingNodeConfig,
  approvalNodeConfig,
  subProcessNodesConfig,
  autoNodesConfig,
  parallelBranchNodesConfig,
  ifBranchNodesConfig
} from "./nodeConfig.js";
// 判断开始结束节点是否存在
export function startNodeExist(data) {
  if (!data.nodeList.length > 1) return false;
  let foundItem = data.nodeList.find((item) => item.nodeType === nodeTypeNames.initiatingNode);
  let keyValuePairExists = foundItem !== undefined;
  return keyValuePairExists;
}
export function endNodeExist(data) {
  if (!data.nodeList.length > 1) return false;
  let foundItem = data.nodeList.find((item) => item.nodeType === nodeTypeNames.finishNode);
  let keyValuePairExists = foundItem !== undefined;
  return keyValuePairExists;
}
// 返回唯一标识
export function getUUID() {
  return Math.random().toString(36).substr(3, 4);
}

//  判断是否是16、17节点类型
export function isBranch(val) {
  if (val == nodeTypeNames.parallelBranchNode || val == nodeTypeNames.ifBranchNode) {
    return false;
  } else {
    return true;
  }
}
// 左边位置
export function nodeLeft(i, left) {
  if (i == 0) {
    return (left = left - 200 > 0 ? left - 200 + "px" : 0 + "px");
  }
  return (left = left + 32 + "px");
}
export function addConfig(nodeType) {
  let config = {};
  switch (nodeType) {
    case nodeTypeNames.initiatingNode: //发起
      config = initiatingNodeConfig;
      break;
    case nodeTypeNames.dispatchNode: //派工
      config = dispatchNodeConfig;
      break;
    case nodeTypeNames.operatorNode: //经办
      config = handlingNodeConfig;
      break;
    case nodeTypeNames.approvalNode: //审批
      config = approvalNodeConfig;
      break;
    case nodeTypeNames.subprocessNode: //子流程
      config = subProcessNodesConfig;
      break;
    case nodeTypeNames.autoNode: //自动
      config = autoNodesConfig;
      break;
    case nodeTypeNames.parallelBranchNode: //并行
      config = parallelBranchNodesConfig;
      break;
    case nodeTypeNames.ifBranchNode: //条件
      config = ifBranchNodesConfig;
      break;
    default:
      break;
  }
  return config;
}
// 对node的name进行处理
export function nodeNameChange(node) {
  switch (node.nodeType) {
    case nodeTypeNames.initiatingNode: //发起
      node.nodeName = "发起节点";
      break;
    case nodeTypeNames.dispatchNode: //派工
      node.nodeName = "派工节点";
      break;
    case nodeTypeNames.operatorNode: //经办
      node.nodeName = "经办节点";
      break;
    case nodeTypeNames.approvalNode: //审批
      node.nodeName = "审批节点";
      break;
    case nodeTypeNames.subprocessNode: //子流程
      node.nodeName = "子流程节点";
      break;
    case nodeTypeNames.autoNode: //自动
      node.nodeName = "自动节点";
      break;
    case nodeTypeNames.parallelBranchNode: //并行
      node.nodeName = "并行分支节点";
      break;
    case nodeTypeNames.ifBranchNode: //条件
      node.nodeName = "条件分支节点";
      break;
    default:
      break;
  }
}
// 对node的type进行处理
export function nodeTypeProcessing(dataSource) {
  dataSource.nodeList.forEach((element, i, arr) => {
    element.show = true;
    element.id = element.nodeKey;
    element.name = element.nodeName;
    element.left = element.left;
    element.top = element.top;
    switch (element.nodeType) {
      case nodeTypeNames.initiatingNode:
        element.type = "发起节点";
        break;
      case nodeTypeNames.dispatchNode:
        element.type = "派工节点";
        break;
      case nodeTypeNames.operatorNode:
        element.type = "经办节点";
        break;
      case nodeTypeNames.approvalNode:
        element.type = "审批节点";
        break;
      case nodeTypeNames.subprocessNode:
        element.type = "子流程节点";
        break;
      case nodeTypeNames.autoNode:
        element.type = "自动节点";
        break;
      case nodeTypeNames.parallelBranchNode:
        element.type = "并行分支节点";
        break;
      case nodeTypeNames.ifBranchNode:
        element.type = "条件分支节点";
        break;
      case nodeTypeNames.finishNode:
        element.type = "结束事件";
        break;
      default:
    }
  });
  dataSource.lineList.forEach((element, i, arr) => {
    element.from = element.fromKey;
    element.to = element.toKey;
  });
  return dataSource;
}
/**
 * 解决回显问题
 * */
export function reload(data) {
  Object.keys(data).map((key) => {
    if (typeof data[key] == "number") {
      data[key] = data[key].toString(); //转为String 是为了上面选择器回显
    }
  });
  return data;
}
// 处理数据
// 经办节点中派工节点
export function operatNodeConversion(data) {
  let option = [];
  let result = data.nodeList.filter((item) => {
    return item.nodeType == nodeTypeNames.dispatchNode;
  });
  result.forEach((item) => {
    option.push({
      value: item.nodeKey,
      label: item.nodeName
    });
  });
  console.log(result, option);
  return { result, option };
}
// 经办节点中派工字段
export function operaLabelConversion(data, val) {
  console.log("------", data, val);
  let node = data.filter((item) => {
    return item.nodeKey == val;
  });
  let options = [];
  node[0].config.dispatch.dispatchSet.forEach((item) => {
    options.push({
      value: item.describe,
      label: item.describe
    });
  });
  console.log("options:", options);
  return options;
}
// 下拉选择字段关系
export function relationship(val) {
  if (val === "input" || !val) return clumn1;
  if (val === "number") return clumn2;
  if (val === "date") return clumn3;
  if (val === "switch") return [{ value: "", label: "等于" }];
  if (val === "raido" || val == "select" || val == "radio") return clumn4;
  if (val === "check" || val == "select") return clumn5;
}
